package com.kl.modular.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.kl.common.exception.SystemException;
import com.kl.common.pager.PageParameter;
import com.kl.common.pager.Pager;
import com.kl.modular.system.dao.bo.SystemRoleAuthBo;
import com.kl.modular.system.dao.mapper.SystemRoleAuthMapper;
import com.kl.modular.system.dao.model.SystemRoleAuth;
import com.kl.modular.system.service.api.ISystemRoleAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色与权限关系serviceImpl业务实现
 * @author XRP_WJC
 * @date 2016-08-05 22:11
 * @(C) XIGNSOFT Copyright 
 */
@Service
public class SystemRoleAuthServiceImpl implements ISystemRoleAuthService {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemRoleAuthServiceImpl.class);
	
	@Autowired
	private SystemRoleAuthMapper systemRoleAuthMapper;
	
	/**
	 * 角色与权限关系列表分页
	 */
	public Pager<SystemRoleAuth> getListByBoPage(SystemRoleAuthBo systemRoleAuthBo, PageParameter pageParameter){
		pageParameter.setPageBoolean(true);
		List<SystemRoleAuth> systemRoleAuthList = systemRoleAuthMapper.getListByBoPage(systemRoleAuthBo, pageParameter);
		return new Pager<SystemRoleAuth>(pageParameter, systemRoleAuthList);
	}
	
	/**
	 * 添加角色与权限关系
	 */
	public void addSystemRoleAuth(SystemRoleAuth systemRoleAuth) throws Exception {
		int result = systemRoleAuthMapper.insert(systemRoleAuth);
		if(result == 0){
			logger.error("添加角色与权限关系发生错误>>data:" + JSON.toJSONString(systemRoleAuth));
			throw new SystemException("添加角色与权限关系发生错误");
		}
	}

	/**
	 * 更新角色与权限关系
	 */
	public void updateByCode(SystemRoleAuth systemRoleAuth) throws Exception {
		int result = systemRoleAuthMapper.updateByPrimaryKey(systemRoleAuth);
		if(result ==0){
			logger.error("更新角色与权限关系发生错误>>data:" + JSON.toJSONString(systemRoleAuth));
			throw new SystemException("更新角色与权限关系发生错误");
		}
	}
	
	/**
	 * 删除角色与权限关系
	 */
	public void deleteByCode(String[] codes) throws Exception {
		int result = systemRoleAuthMapper.deleteByPrimaryKey(codes);
		if(result == 0){
			logger.error("删除角色与权限关系发生错误>>data:" + JSON.toJSONString(codes));
			throw new SystemException("删除角色与权限关系发生错误");
		}
	}
	
	/**
	 * 查询角色与权限关系
	 */
	public SystemRoleAuth getSystemRoleAuthByCode(String code) throws Exception {
		SystemRoleAuth systemRoleAuth = systemRoleAuthMapper.selectByPrimaryKey(code);
		return systemRoleAuth;
	}

	/**
	 * 角色权限
	 */
	public List<SystemRoleAuth> getListByBo(SystemRoleAuthBo bo) {
		PageParameter pageParamter = new PageParameter();
		pageParamter.setPageBoolean(false);
		return systemRoleAuthMapper.getListByBoPage(bo, pageParamter);
	}
	
	/**
	 * 物理删除
	 */
	public int deleteWlSystemRoleAuth(String roleCode) {
		return systemRoleAuthMapper.delSystemRoleAuth(roleCode);
	}
	
}
